<?php

/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not 
 *  exist, see <http://www.gnu.org/licenses/>.
 * 
 * $LastChangedDate: 2008-07-09 20:09:07 +0200 (Wed, 09 Jul 2008) $
 * $Rev: 1278 $
 * $Author: gavinspearhead $
 * $Id: admin_buttons.php 1278 2008-07-09 18:09:07Z gavinspearhead $
 */


define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathvb = realpath(dirname(__FILE__));

require_once "$pathvb/../functions/html_includes.php" ;

if (!$isadmin)
	fatal_error($LN['error_noadmin']);

$title = 'URD - ' . $LN['buttons_title'];

class buttons_c {
	public $type; // "show" | "edit"
	public $name;
	public $url;
	public $img;
	public $id;

	function __construct ($t, $id=0, $n='', $u='', $i='')
	{
		assert(is_numeric($id) && ($t == 'show' || $t == 'edit'));
		$this->id = $id;
		$this->type = $t;
		$this->name = $n;
		$this->url = $u;
		$this->img = $i;
	}
}

$edit = NULL;
$buttons = array ();

if (isset($_POST['action']) && isset($_POST['action_id']) && is_numeric($_POST['action_id'])) {
	verify_challenge($_POST['challenge']);
	if ($_POST['action'] == 'edit') 
		$edit = $_POST['action_id'];
	else if ($_POST['action'] == 'delete') {
		delete_button($db, $_POST['action_id']);
		header('Location: admin_buttons.php');
	} else
		fatal_error($LN['buttons_nobuttonid']);
}

if (isset ($_POST['apply'])) {
	verify_challenge($_POST['challenge']);
	if (isset($_POST['name']) && trim($_POST['name']) != '') 
		$name = trim($_POST['name']);
	else
		fatal_error($LN['buttons_invalidname']);
	if (isset($_POST['image_url']) && $_POST['image_url'] != '') 
		$image_url = $_POST['image_url'];
	else
		fatal_error($LN['buttons_invalidimg']);
	if (isset($_POST['search_url']) && $_POST['search_url'] != '') 
		$search_url = $_POST['search_url'];
	else
		fatal_error($LN['buttons_invalidurl']);
	if (isset($_POST['id']) && $_POST['id'] != '') 
		$id = $_POST['id'];
	else
		fatal_error($LN['buttons_invalidurl']);
	$query = "SELECT \"id\" FROM searchbuttons WHERE \"id\"='$id'";
	$res = $db->execute_query($query);
	if ($res !== FALSE) {
		update_button($db, $name, $image_url, $search_url, $id);
		header('Location: admin_buttons.php');
	} else 
		fatal_error('A button with that name does not exists');
} else if (isset ($_POST['add'])) {
	verify_challenge($_POST['challenge']);

	if (isset($_POST['name']) && trim($_POST['name']) != '') 
		$name = trim($_POST['name']);
	else
		fatal_error($LN['buttons_invalidname']);
	if (isset($_POST['image_url']) && $_POST['image_url'] != '') 
		$image_url = $_POST['image_url'];
	else
		fatal_error($LN['buttons_invalidimg']);
	if (isset($_POST['search_url']) && $_POST['search_url'] != '') 
		$search_url = $_POST['search_url'];
	else
		fatal_error($LN['buttons_invalidurl']);
	$query = "SELECT \"id\" FROM searchbuttons WHERE \"name\"='$name'";
	$res = $db->execute_query($query);
	if ($res === FALSE) {
		add_button($db, $name, $image_url, $search_url);
		header('Location: admin_buttons.php');
	} else 
        fatal_error($LN['buttons_buttonexists']);
} else if (isset($_REQUEST['cmd'])  && $_REQUEST['cmd'] == 'export_settings') {
    header('Content-Type: text/html/force-download');
    header('Content-Disposition: attachment; filename=urd_buttons_settings.xml');
    $xml = new urd_xml_writer('php://output');
    $xml->write_buttons($db);
    $xml->output_xml_data();
    die;
} else if (isset($_POST['cmd'])  && $_POST['cmd'] == 'import_settings') {
    verify_challenge($_POST['challenge']);
    try {
        $xml = new urd_xml_reader($_FILES['filename']['tmp_name']);
        $buttons = $xml->read_buttons($db);
        if ($buttons != array()) {
            clear_all_buttons($db);
            set_all_buttons($db, $buttons);
            redirect('admin_buttons.php');
        }
        die_html('No buttons found');
    } catch (exception $e) {
    }
} else {
    try {
        $res = get_all_buttons($db);
    } catch (exception $e) {
        $res = array();
    }
	$cnt = 0;
	foreach ($res as $row) {
		$name = $row['name'];
		$image_url =  $row['image_url'];
		$search_url = $row['search_url'];
		$id = $row['id'];
		if ($edit == $id)
			$buttons[] = new buttons_c('edit', $id, $name, $search_url, $image_url);
		else
			$buttons[] = new buttons_c('show', $id, $name, $search_url, $image_url);
	}
	if ($edit === NULL) 
		$buttons[] = new buttons_c('edit');
}


$challenge = set_challenge();

$smarty->assign('USER_ADMIN',   USER_ADMIN);
$smarty->assign('USER_ACTIVE',  USER_ACTIVE);
$smarty->assign('USER_PENDING', USER_PENDING);
$smarty->assign('menu',		    generate_menu($LN));
$smarty->assign('title', 	    $title);
$smarty->assign('buttons',	    $buttons);
$smarty->assign('maxstrlen',	$prefs['maxsetname']);
$smarty->assign('challenge',    $challenge);
$smarty->assign('referrer',   	basename(__FILE__, '.php'));
$smarty->display('admin_buttons.tpl');


?>
